﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SAM.Framework.Javascript;
using SAM.Framework.MockData;

namespace SAM.MockData
{
    public class DefaultMockData : IMock
    {
        /// <summary>
        /// 执行引擎扩展
        /// </summary>
        private IExchange exchange;
        /// <summary>
        /// 执行引擎的执行接口
        /// </summary>
        private IExecute execute;

        /// <summary>
        /// 返回值名称
        /// </summary>
        protected string resultName = "result";


        public DefaultMockData(IExecute execute , IExchange exchange)
        {
            this.exchange = exchange;
            this.execute = execute;
        }


        public T Mock<T>(string codeStr)
        {
            MockContext<T> mockContext = new MockContext<T>();
            mockContext.CodeStr = string.Format("var _{1}=Mock.mock( {0} )", codeStr, resultName);
            mockContext.Result = Activator.CreateInstance<T>();
            mockContext.ResultName = resultName;
            this.exchange.AddHostObject(resultName, mockContext.Result);
            IMockHandler<T> handler = new Handlers.MockHandlerForType<T>();
            handler.Handler(mockContext);
            this.execute.ExecuteNonResult(mockContext.CodeStr);
            return mockContext.Result;
        }
    }
}
